package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.StbCompany;
import com.ruoyi.system.domain.StbEmployee;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.IStbCompanyService;
import com.ruoyi.system.service.IStbEmployeeService;
import com.ruoyi.system.service.ISysDictDataService;

/**
 * 员工Controller
 * 
 * @author ruoyi
 * @date 2020-02-08
 */
@Controller
@RequestMapping("/system/employee")
public class StbEmployeeController extends BaseController
{
    private String prefix = "system/employee";

    @Autowired
    private IStbEmployeeService stbEmployeeService;

    /*
     * 企业业务逻辑类
     */
    @Autowired
    private IStbCompanyService stbCompanyService;
    
    @Autowired
    private ISysDictDataService dictDataService;
    
    @RequiresPermissions("system:employee:view")
    @GetMapping()
    public String employee(ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        return prefix + "/employee";
    }
    
	/*
	 * 查询人才库  empname:员工名称  emptabs:员工标签 
	 */
    @PostMapping("/istalent")
    @ResponseBody
    public List<Map<String,Object>> selectIsTalent(){
    	SysDictData dictData=new SysDictData();
    	dictData.setDictType("talent_tabs");
    	List<SysDictData> dictList = dictDataService.selectDictDataList(dictData);
    	List<Map<String, Object>> resultMapList = new LinkedList<Map<String,Object>>();
    	List<StbEmployee> empList = stbEmployeeService.selectStbEmployeeList(new StbEmployee());
    	List<StbEmployee> newEmpList = empList.stream()
    			.filter(t->t.getEmpTabs()!=null&&!t.getEmpTabs().equalsIgnoreCase(""))
    			.collect(Collectors.toList());
    	List<StbCompany> companyList = stbCompanyService.selectStbCompanyList(new StbCompany());    	
    	for (StbEmployee currentEmp : newEmpList) {
    		Map<String,Object> map = new HashMap<String,Object>();
    		map.put("empname", currentEmp.getEmpName());
    		String employeeTabIds = currentEmp.getEmpTabs();
    		//1,2
    		List<SysDictData> dictCollect = dictList.stream()
	 			.filter(t->{
	 				String[] idArr = employeeTabIds.split(",");
	 				List<String> idList = Arrays.asList(idArr);
	 				return idList.contains(t.getDictValue());
	 			})
	 			.collect(Collectors.toList());
    		List<String> tabNameList=new ArrayList<>();
    		for (SysDictData sysDictData : dictCollect) {
				String dictLabel = sysDictData.getDictLabel();
				tabNameList.add(dictLabel);
			}
    		Object [] tabNameArr = tabNameList.toArray();
    		String  employeetabs = StringUtils.join(tabNameArr, ",");
    		map.put("employeetabs", employeetabs);
    		//绑定公司名称
    		List<StbCompany> currentCompList = companyList.stream()
    			.filter(t->t.getCompId().equals(currentEmp.getCompId()))
    			.collect(Collectors.toList());
    		if(com.ruoyi.common.utils.StringUtils.isNotEmpty(currentCompList)) {
    			StbCompany currentComp = currentCompList.get(0);
    			map.put("compName", currentComp.getCompName());
    		}
    		resultMapList.add(map);
		}
	return resultMapList;
    }
    /*
	 * 查询各类员工标签数量  name:名称 num:数量
	 */
    @PostMapping("/tabscount")
    @ResponseBody
    public List<Map<String,Object>> selectTabsCount(){
    	SysDictData dictData=new SysDictData();
    	dictData.setDictType("talent_tabs");
    	List<SysDictData> list = dictDataService.selectDictDataList(dictData);
    	List<Map<String, Object>> resultMapList = new LinkedList<Map<String,Object>>();
    	StbEmployee stbEmployee = new StbEmployee();
    	List<StbEmployee> empList = stbEmployeeService.selectStbEmployeeList(stbEmployee);
    	for (SysDictData sysDictData : list) {
    		Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", sysDictData.getDictLabel());
			Integer num=0;
			String key=sysDictData.getDictValue();
			for (StbEmployee item : empList) {
				String emptabs = item.getEmpTabs();
				if(emptabs!=null&&!emptabs.equals("")) {
					String[] keyArr = emptabs.split(",");
					List<String> keyList= new ArrayList<>(Arrays.asList(keyArr));
					if(keyList.contains(key)) {
						num++;
					}
				}
			}
			map.put("num",num);
			resultMapList.add(map);
		}
    	return resultMapList;
    }
    
    /**
     * 查询员工列表
     * @param stbCompany 
     */
    @RequiresPermissions("system:employee:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StbEmployee stbEmployee)
    {
        startPage();
        List<StbEmployee> list = stbEmployeeService.selectStbEmployeeList(stbEmployee);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @RequiresPermissions("system:employee:export")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StbEmployee stbEmployee)
    {
        List<StbEmployee> list = stbEmployeeService.selectStbEmployeeList(stbEmployee);
        ExcelUtil<StbEmployee> util = new ExcelUtil<StbEmployee>(StbEmployee.class);
        return util.exportExcel(list, "employee");
    }

    /**
     * 新增员工
     * @param employee 
     * @param stbEmployee 
     */
    @GetMapping("/add")
    public String add(ModelMap mmap, BaseEntity employee, StbEmployee stbEmployee)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        String [] tabIds = stbEmployee.getEmpTabs()==null?new String [0]:stbEmployee.getEmpTabs().split(",");
        stbEmployee.setTabIds(tabIds);
        return prefix + "/add";
    }

    /**
     * 新增保存员工
     */
    @RequiresPermissions("system:employee:add")
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StbEmployee stbEmployee)
    {
    	if(stbEmployee.getEmpPhone()!=null&&!stbEmployee.getEmpPhone().equals("")) {
    		StbEmployee obj = new StbEmployee();
        	obj.setEmpPhone(stbEmployee.getEmpPhone());
        	List<StbEmployee> employeeList = stbEmployeeService.selectStbEmployeeList(obj);
        	if(employeeList!=null&&employeeList.size()>0) {
    	        return AjaxResult.error("该手机号已被其他用户使用!");
        	}
    	}
    	stbEmployee.setCreateBy(ShiroUtils.getLoginName());
    	stbEmployee.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(stbEmployeeService.insertStbEmployee(stbEmployee));
    }

    /**
     * 修改员工
     */
    @GetMapping("/edit/{empId}")
    public String edit(@PathVariable("empId") Long empId, ModelMap mmap)
    {
    	StbCompany stbCompany = new  StbCompany();
    	List<StbCompany> list = stbCompanyService.selectStbCompanyList(stbCompany);
    	mmap.put("companyList",list);
        StbEmployee stbEmployee = stbEmployeeService.selectStbEmployeeById(empId);
        mmap.put("stbEmployee", stbEmployee);
        String [] tabIds = stbEmployee.getEmpTabs()==null?new String [0]:stbEmployee.getEmpTabs().split(",");
        stbEmployee.setTabIds(tabIds);
        return prefix + "/edit";
    }

    /**
     * 修改保存员工
     */
    @SuppressWarnings("null")
    @RequiresPermissions("system:employee:edit")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StbEmployee stbEmployee)
    {
    	stbEmployee.setUpdateBy(ShiroUtils.getLoginName());
    	stbEmployee.setEmpTabs(StringUtils.join(stbEmployee.getTabIds(),","));
    	if(stbEmployee.getEmpPhone()!=null&&!stbEmployee.getEmpPhone().equals("")) {
    		StbEmployee obj = new StbEmployee();
        	obj.setEmpPhone(stbEmployee.getEmpPhone());
        	List<StbEmployee> employeeList = stbEmployeeService.selectStbEmployeeList(obj);
        	int flag=0;
        	if(employeeList==null&&employeeList.size()==0) {
        		flag=1;
        	}else if(employeeList.size()==1) {
        		StbEmployee stbEmployee2 = employeeList.get(0);
        		if(stbEmployee2.getEmpId().equals(stbEmployee.getEmpId())) {
        			flag=1;
        		}else {
        			flag=2;
        		}
        	}
    		if(flag==1) {
    	        return toAjax(stbEmployeeService.updateStbEmployee(stbEmployee));
    		}else if(flag==2) {
    			return AjaxResult.error("该手机号已被其他用户使用!");
    		}else {
    			return AjaxResult.error("该手机号已被多个用户使用！");
    		}
    	}else {
    		return toAjax(stbEmployeeService.updateStbEmployee(stbEmployee));
    	}
    	
    }

    /**
     * 删除员工
     */
    @RequiresPermissions("system:employee:remove")
    @Log(title = "员工", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stbEmployeeService.deleteStbEmployeeByIds(ids));
    }
}
